﻿Imports System.IO
Imports System.Data.OleDb
Public Class frmNewUser
    Dim cnn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim d As Double
    Dim f As String
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'Try
        If txtUserLogin.Text = "" Then
            MsgBox("Please, Input Username ! ", MsgBoxStyle.Information, "HRIMS System Error")
            txtUserLogin.Focus()
        ElseIf txtPassword.Text = "" Then
            MsgBox("Please, Input Password !", MsgBoxStyle.Information, "HRIMS System Error")
            txtPassword.Focus()
        ElseIf txtConfirm.Text = "" Then
            MsgBox("Please, Input Confirm Password !", MsgBoxStyle.Information, "HRIMS System Error")
            txtConfirm.Focus()
        ElseIf txtPosition.Text = "" Then
            MsgBox("Please, Input Postition !", MsgBoxStyle.Information, "HRIMS System Error")
            txtPosition.Focus()
        ElseIf txtPassword.Text <> txtConfirm.Text Then
            MsgBox("Password and Confirm Password not Match !", MsgBoxStyle.Information, "HRIMS System Error")
            txtPassword.Text = ""
            txtConfirm.Text = ""
            txtPassword.Focus()
        Else
            Dim SQL As String = "INSERT INTO tbl_users(UserID,KFullName,LFullName,UserLogin,UserPass,CreateDate,UpdateDate,[Position])" & _
                            "VALUES (" & Me.txtUserID.Text & ",'" & Me.txtKFullName.Text & "','" & Me.txtEFullName.Text & "','" & Me.txtUserLogin.Text & _
                            "','" & Me.txtPassword.Text & "','" & FormatDateTime(Now, DateFormat.ShortDate) & "','" & FormatDateTime(Now, DateFormat.ShortDate) & "','" & txtPosition.Text & " ')"

            SaveDataToAccess(SQL)
            ClearText()
            LoadDataToLVUsers("SELECT * FROM tbl_users WHERE Status <> '1'", frmUserControls.LVUsers)
            frmUserControls.LVUsers.Refresh()

        End If
        'Catch ex As Exception
        ' '   MsgBox(ex, MsgBoxStyle.Information, "HRIMS System Error")
        'End Try
    End Sub
    Sub SaveDataToAccessDB()
        Try
            Dim cn As New OleDb.OleDbConnection
            cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\hrims.accdb"
            cn.Open()
            Dim myCmd As New OleDb.OleDbCommand
            myCmd.Connection = cn
            myCmd.CommandText = "INSERT INTO tbl_users(UserID,KFullName,LFullName,UserLogin,UserPass,CreateDate,UpdateDate,[Position])" & _
                            "VALUES (" & Me.txtUserID.Text & ",'" & Me.txtKFullName.Text & "','" & Me.txtEFullName.Text & "','" & Me.txtUserLogin.Text & _
                            "','" & Me.txtPassword.Text & "','" & FormatDateTime(Now, DateFormat.ShortDate) & "','" & FormatDateTime(Now, DateFormat.ShortDate) & "','" & txtPosition.Text & " ')"
            myCmd.ExecuteNonQuery()
            cn.Close()
            MsgBox("Data Save successfully!")
            ClearText()

        Catch ex As Exception
            MsgBox(ex, MsgBoxStyle.Information, "'HRIMS System Error")
        End Try
    End Sub
    Private Sub frmNewUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Edit = True Then
            LoadData2AddUser("SELECT * FROM tbl_users WHERE UserID=" & PUserID, Me)
            btnSave.Enabled = False
        Else
            txtUserID.Enabled = False
            txtUserID.ReadOnly = True
            Connect()
            Me.txtUserID.Text = AutoID4Access("SELECT UserID FROM tbl_users", "tbl_users") + 1
        End If
       
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        ClearText()
        btnSave.Enabled = True
        btnEdit.Enabled = False

        Me.txtUserID.Text = AutoID4Access("SELECT UserID FROM tbl_users", "tbl_users") + 1
    End Sub

    Sub ClearText()
        Me.txtUserID.Text = ""
        Me.txtUserLogin.Text = ""
        Me.txtKFullName.Text = ""
        Me.txtPassword.Text = ""
        Me.txtPosition.Text = ""
        Me.txtEFullName.Text = ""
        Me.txtConfirm.Text = ""

        Me.txtKFullName.Focus()

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim SQL As String = "UPDATE tbl_users SET KFullName='" & Me.txtKFullName.Text & _
                                            "', LFullName='" & Me.txtEFullName.Text & _
                                            "',UserLogin='" & Me.txtUserLogin.Text & _
                                            "',UserPass='" & Me.txtPassword.Text & _
                                            "',UpdateDate='" & FormatDateTime(Now, DateFormat.ShortDate) & _
                                            "',[Position]='" & txtPosition.Text & "' WHERE UserID=" & Me.txtUserID.Text
        SaveDataToAccess(SQL)
        ClearText()
    End Sub
End Class
